<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    fullscreen
    center append-to-body>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="凭证类型" prop="type">
      <el-select v-model="dataForm.type" @change="typeChange">
        <el-option label="付款单" value="1"></el-option>
        <el-option label="收款单" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="凭证类型" prop="twoType">
      <el-select v-model="dataForm.twoType">
        <el-option label="付款单--发票" :value="1" v-if="dataForm.type == 1"></el-option>
        <el-option label="付款单--单据" :value="2" v-if="dataForm.type == 1"></el-option>
        <el-option label="预付款" :value="3" v-if="dataForm.type == 1"></el-option>
        <el-option label="厂家减账" :value="4" v-if="dataForm.type == 1"></el-option>
        <el-option label="其他付款" :value="5" v-if="dataForm.type == 1"></el-option>
        <el-option label="调账" :value="6" v-if="dataForm.type == 1"></el-option>
        <el-option label="客户批发" :value="7" v-if="dataForm.type == 2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="memo">
      <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
    </el-form-item>

    <el-tabs type="border-card">
      <el-divider content-position="left">详细信息</el-divider>
      <el-row>
        <el-button type="primary" icon="el-icon-plus" @click.prevent="addRow()">添加科目</el-button>
        <el-button type="primary" icon="el-icon-delete" @click.prevent="delData()">删除</el-button>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataForm.detailList" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="direct" header-align="center" align="center" label="方向">
            <template slot-scope="scope">
              <el-select v-model="scope.row.direct">
                <el-option label="借" value="1"></el-option>
                <el-option label="贷" value="2"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="subjectName" header-align="center" align="center" label="科目名称">
            <template slot-scope="scope">
              <el-input v-model="scope.row.subjectName" placeholder="科目名称" readonly>
                <el-button slot="append" @click="checkParent(scope.row)">选择</el-button>
              </el-input>
            </template>
          </el-table-column>
          <el-table-column prop="formula" header-align="center" align="center" label="计算方式">
            <template slot-scope="scope">
              <el-select v-model="scope.row.formula" clearable>
                <el-option label="付款金额" :value="1"></el-option>
                <el-option label="收款金额" :value="2"></el-option>
                <el-option label="预付款及冲账金额" :value="3"></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
    <!-- 弹窗, 隶属科目查询 -->
    <parent-choose v-if="parentVisible" ref="parentDialog" @refreshData="getParentData"></parent-choose>
  </el-dialog>



</template>

<script>
    import {getObj, addObj, putObj} from '@/api/eims/evidencetemplate'
    import ParentChoose from '../subject/parentChoose'

    export default {
    data () {
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id: 0,
          name: '',
          type: '',
          twoType: '',
          memo: '',
          status: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          orgId: '',
          tenantId: '',
          unitId: '',
          delFlag: '',
          detailList:[],
        },
        parentVisible: false,
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '凭证类型不能为空', trigger: 'blur' }
          ],
          twoType: [
            { required: true, message: '详细类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      ParentChoose
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        if(this.dataForm.id == 0){
          this.dataForm.detailList = []
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            } else {
                addObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      // 选择事件 得到选中的数据
      handleSelectionChange(data) {
        this.selectlistRow = data
      },
      addRow() {
        var timestamp = new Date().getTime()
        var list = {
          timestamp: timestamp,
          direct: '1',
          subjectName: '',
          formula: 1,
        };
        this.dataForm.detailList.push(list)
      },
      //  删除
      delData() {
        this.selectlistRow.forEach((item, index) => {
          for (let i = 0; i < this.dataForm.detailList.length; i++) {
            if (this.dataForm.detailList[i].timestamp === item.timestamp) {
              this.dataForm.detailList.splice(i, 1)
            }
          }
          this.selectlistRow = []
        })
      },
      checkParent(row){
        this.parentVisible = true
        this.$nextTick(() => {
          this.$refs.parentDialog.init(row.timestamp);
        });
      },
      getParentData(data){
        let subjectId = data.id
        let subjectName = ""+data.code + " " + data.name

        this.dataForm.detailList.forEach(item => {
          if(item.timestamp === data.timestamp){
            item.subjectName = subjectName
          }
        })
      },
      typeChange(){
        this.$set(this.dataForm,'twoType','')
      }
    }
  }
</script>
